<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:252px;">
                    <div class="user-info">
                        <img src="../../assets/img/cat.jpg" class="user-avator" alt />
                        <div class="user-info-cont">
                            <div class="user-info-name">{{name}}</div>
                            <div>{{role}}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        上次登录时间：
                        <span>{{ gettime }}</span>
                        <!-- <span>2022-11-05</span> -->
                    </div>
                    <div class="user-info-list">
                        上次登录地点：  
                        <span>重庆</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height:252px;">
                    <div slot="header" class="clearfix">
                        <span>学习人群</span>
                    </div>
                    web前端
                        <el-progress :percentage="71.3" color="#9787ff"></el-progress>
                    后端
                        <el-progress :percentage="24.1" color="#f1e05a"></el-progress>
                    客户端
                        <el-progress :percentage="13.7"></el-progress>
                    其他
                        <el-progress :percentage="5.9" color="#f56c6c"></el-progress>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-lx-people grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">756千万</div>
                                    <div>用户访问量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-lx-notice grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">125万</div>
                                    <div>系统消息</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-reading grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">364万</div>
                                    <div>学习人数</div>
                                </div>
                            </div>
                        </el-card>  
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height:403px;">
                    <div slot="header" class="clearfix">
                        <span>本周学习任务</span>
                        <el-button style="float: right; padding: 3px 0" type="text">添加</el-button>
                    </div>
                    <el-table :show-header="false" :data="todoList" style="width:100%;">
                        <el-table-column width="40">
                            <template slot-scope="scope">
                                <el-checkbox v-model="scope.row.status"></el-checkbox>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template slot-scope="scope">
                                <div
                                    class="todo-item"
                                    :class="{'todo-item-del': scope.row.status}"
                                >{{scope.row.title}}</div>
                            </template>
                        </el-table-column>
                        <el-table-column width="60">
                            <template>
                                <i class="el-icon-edit"></i>
                                <i class="el-icon-delete" @click="handleDel"></i>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
                <el-card shadow="hover">
                    <div ref="echarts1" style="height: 280px"></div>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card shadow="hover">
                    <div ref="echarts2" style="height: 280px"></div>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import Schart from 'vue-schart';
import bus from '../common/bus';
import * as echarts from 'echarts'
export default {
    name: 'dashboard',
    data() {
        return {
            gettime: "",
            name: localStorage.getItem('ms_username'),
            todoList: [
                {
                    title: '预约讲解vue框架，react框架和Angular框架',
                    status: false
                },
                {
                    title: '学习springBoot全家桶并完成开发项目小练习',
                    status: false
                },
                {
                    title: 'vue脚手架配置路由并进行mock，Ajax数据处理',
                    status: false
                },
                {
                    title: 'python处理数据的可视化，完成可视化展示',    
                    status: false
                },
                {
                    title: '了解客户端开发知识体系及用户交互问题',
                    status: true
                },
                {
                    title: '学员按要求分组参加此次程序设计大赛',
                    status: true
                }
            ],
            data: [
                {
                    name: '2022/10/14',
                    value: 1083
                },
                {
                    name: '2022/11/15',
                    value: 941
                },
                {
                    name: '2022/10/16',
                    value: 1139
                },
                {
                    name: '2022/10/22',
                    value: 816
                },
                {
                    name: '2022/11/22',
                    value: 327
                },
                {
                    name: '2022/10/12',
                    value: 228
                },
                {
                    name: '2022/10/18',
                    value: 1065
                }
            ],
        };
    },
    components: {
        Schart
    },
    computed: {
        role() {
            return this.name === 'admin' ? '超级管理员' : '普通用户';
        }
    },
    // created() {
    //     this.handleListener();
    //     this.changeDate();
    // },
    // activated() {
    //     this.handleListener();
    // },
    // deactivated() {
    //     window.removeEventListener('resize', this.renderChart);
    //     bus.$off('collapse', this.handleBus);
    // },
    methods: {
        handleDel(index) {
            const item = this.todoList.splice(index, 1);
            console.log(item);
        },
        changeDate() {
            const now = new Date().getTime();
            this.data.forEach((item, index) => {
                const date = new Date(now - (6 - index) * 86400000);
                item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
            });
        },
        // handleListener() {
        //     bus.$on('collapse', this.handleBus);
        //     // 调用renderChart方法对图表进行重新渲染
        //     window.addEventListener('resize', this.renderChart);
        // },
        // handleBus(msg) {
        //     setTimeout(() => {
        //         this.renderChart();
        //     }, 200);
        // },
        // renderChart() {
        //     this.$refs.bar.renderChart();
        //     this.$refs.line.renderChart();
        // }
        getTime() {
            var _this = this;
            let yy = new Date().getFullYear();
            var mm =
                new Date().getMonth() > 9
                ? new Date().getMonth() + 1
                : new Date().getMonth() == 9
                ? new Date().getMonth() + 1
                : '0' + (new Date().getMonth() + 1);
            var dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate();
            let hh = new Date().getHours();
            let mf =
                new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
            let ss =
                new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
            // _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;
            _this.gettime = yy + '-' + mm + '-' + dd;
            },
            currentTime() {
            setInterval(this.getTime, 10);
        },
    },
    mounted() {
        this.currentTime();
        
        // 饼状图
        const echarts1 = echarts.init(this.$refs.echarts1)      
        var trafficWay = [
        { name: 'java' , value: 20,},
        { name: 'python' , value: 54,},
        { name: 'JavaScript' , value: 32,},
        { name: 'c++' , value: 45,},
        { name: 'vue' , value: 23 },
        { name: 'react', value: 52 },
        ];
        var data = [];
        var color = ['#fd566a', '#9787ff', '#fdb36a', '#fdd56a', '#6da7ff', '#63e1f2', '#ff3000'];
        for (var i = 0; i < trafficWay.length; i++) {
        data.push(
          {
            value: trafficWay[i].value,
            name: trafficWay[i].name,
            itemStyle: {
              normal: {
                borderWidth: 5,
                shadowBlur: 20,
                borderColor: color[i],
                shadowColor: color[i],
              },
            },
          },
          {
            value: 2,
            name: '',
            itemStyle: {
              normal: {
                label: {
                  show: false,
                },
                labelLine: {
                  show: false,
                },
                color: 'rgba(0, 0, 0, 0)',
                borderColor: 'rgba(0, 0, 0, 0)',
                borderWidth: 0,
              },
            },
          }
        );
        }
        var seriesOption = [
        {
          name: '',
          type: 'pie',
          clockWise: false,
          radius: [105, 109],
          hoverAnimation: false,
          itemStyle: {
            normal: {
              label: {
                show: true,
                position: 'outside',
                formatter: function (params) {
                  var percent = 0;
                  var total = 0;
                  for (var i = 0; i < trafficWay.length; i++) {
                    total += trafficWay[i].value;
                  }
                  percent = ((params.value / total) * 100).toFixed(0);
                  if (params.name !== '') {
                    return params.name + '\t' + percent + '%';
                  } else {
                    return '';
                  }
                },
              },
              labelLine: {
                length: 10,
                length2: 20,
                show: true,
                color: '#00ffff',
              },
            },
          },
          data: data,
        },
        ];
        let option1 = {
        backgroundColor: '#fff',
        color: color,
        title: [{
          text: '技术学习',
          top: '44%',
          textAlign: 'center',
          left: '49.50%',
          backgroundColor: '#fff',
          borderRadius: 100,
          textStyle: {
            color: '#fd566a',
            fontSize: 20,
            fontWeight: '400',
          },
        }],
        tooltip: {
          show: false,
        },

        toolbox: {
          show: false,
        },
        series: seriesOption,
        };
        echarts1.setOption(option1)

        //折线图
        const echarts2 = echarts.init(this.$refs.echarts2)
        let colorList = ['#46ea91', '#2ba0ff', '#ed593b', '#7357ff', '#f2d750'];
        let option2 = {
            backgroundColor: '#fff',
            title: {
            text: '页面浏览',
            // top: '1%',
            left: '10%',
            backgroundColor: '#fff',
            textStyle: {
                color: '#242f42',
                fontSize: 20,
                fontWeight: '400',
            },
            },
            legend: {
                icon: 'circle',
                top: '5%',
                right: '5%',
                itemWidth: 6,
                itemGap: 5,
                textStyle: {
                    color: '#9787ff',
                    padding: [3, 0, 0, 0],
                },
            },
            tooltip: {
                trigger: 'axis',
            },
            grid: {
                top: '15%',
                left: '5%',
                bottom: '10%',
                right: '5%',
            },
            xAxis: [
                {
                    type: 'category',
                    data: ['2022-01', '2022-01', '2022-01', '2022-01', '2022-01', '2022-01', '2022-01', '2022-01'],
                    axisLine: {
                        lineStyle: {
                            color: '#33BBFF',
                        },
                    },
                    axisTick: {
                        show: false,
                    },
                    axisLabel: {
                        interval: 0,
                        textStyle: {
                            color: '#5FBBEB',
                        },
                        // 默认x轴字体大小
                        fontSize: 12,
                        // margin:文字到x轴的距离
                        margin: 10,
                    },
                    axisPointer: {
                        label: {
                            // padding: [11, 5, 7],
                            padding: [0, 0, 0, 0],
                            // 这里的margin和axisLabel的margin要一致!
                            margin: 10,
                            // 移入时的字体大小
                            fontSize: 12,
                            backgroundColor: 'rgba(0,0,0,0)',
                        },
                    },
                    boundaryGap: false,
                },
            ],
            yAxis: [
                {
                    name: '数量 / k',
                    axisTick: {
                        show: false,
                    },
                    axisLine: {
                        show: true,
                        lineStyle: {
                            color: '#05D5FF',
                        },
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#5FBBEB',
                        },
                    },
                    splitLine: {
                        show: false,
                    },
                },
            ],
            series: [
                {
                    name: '首页',
                    type: 'line',
                    data: [100, 20, 30, 102, 15, 30, 20, 18],
                    symbolSize: 1,
                    symbol: 'circle',
                    smooth: true,
                    showSymbol: false,
                    lineStyle: {
                        width: 2,
                        color: new echarts.graphic.LinearGradient(1, 1, 0, 0, [
                            {
                                offset: 0,
                                color: '#90ffc6',
                            },
                            {
                                offset: 1,
                                color: '#46ea91',
                            },
                        ]),
                        shadowColor: 'rgba(144, 255, 198, .3)',
                        shadowBlur: 5,
                        shadowOffsetY: 5,
                    },
                    itemStyle: {
                        normal: {
                            color: colorList[0],
                            borderColor: colorList[0],
                        },
                    },
                },
                {
                    name: '消息数量',
                    type: 'line',
                    data: [20, 12, 11, 14, 25, 16, 10, 20],
                    symbolSize: 1,
                    symbol: 'circle',
                    smooth: true,
                    showSymbol: false,
                    lineStyle: {
                        width: 2,
                        color: new echarts.graphic.LinearGradient(1, 1, 0, 0, [
                            {
                                offset: 0,
                                color: '#67bcfc',
                            },
                            {
                                offset: 1,
                                color: '#2ba0ff',
                            },
                        ]),
                        shadowColor: 'rgba(105, 188, 252,.3)',
                        shadowBlur: 5,
                        shadowOffsetY: 5,
                    },
                    itemStyle: {
                        normal: {
                            color: colorList[1],
                            borderColor: colorList[1],
                        },
                    },
                },
                {
                    name: '信息添加',
                    type: 'line',
                    data: [150, 120, 170, 140, 100, 160, 110, 110],
                    symbolSize: 1,
                    symbol: 'circle',
                    smooth: true,
                    showSymbol: false,
                    lineStyle: {
                        width: 2,
                        color: new echarts.graphic.LinearGradient(1, 1, 0, 0, [
                            {
                                offset: 0,
                                color: '#fc937e ',
                            },
                            {
                                offset: 1,
                                color: '#ed593b',
                            },
                        ]),
                        shadowColor: 'rgb(252, 147, 126,.3)',
                        shadowBlur: 2,
                        shadowOffsetY: 2,
                    },
                    itemStyle: {
                        normal: {
                            color: colorList[2],
                            borderColor: colorList[2],
                        },
                    },
                },
                {
                    name: '数据可视化',
                    type: 'line',
                    data: [200, 80, 100, 30, 60, 50, 110, 20],
                    symbolSize: 1,
                    symbol: 'circle',
                    smooth: true,
                    showSymbol: false,
                    lineStyle: {
                        width: 2,
                        color: new echarts.graphic.LinearGradient(1, 1, 0, 0, [
                            {
                                offset: 0,
                                color: '#a390ff',
                            },
                            {
                                offset: 1,
                                color: '#7357ff',
                            },
                        ]),
                        shadowColor: 'rgba(115, 87, 255, .1)',
                        shadowBlur: 5,
                        shadowOffsetY: 5,
                    },
                    itemStyle: {
                        normal: {
                            color: colorList[3],
                            borderColor: colorList[3],
                        },
                    },
                },
                {
                    name: '销售列表',
                    type: 'line',
                    data: [20, 80, 150, 30, 60, 50, 50, 20],
                    symbolSize: 1,
                    symbol: 'circle',
                    smooth: true,
                    showSymbol: false,
                    lineStyle: {
                        width: 2,
                        color: new echarts.graphic.LinearGradient(1, 1, 0, 0, [
                            {
                                offset: 0,
                                color: '#ffeb86',
                            },
                            {
                                offset: 1,
                                color: '#f2d750',
                            },
                        ]),
                        shadowColor: 'rgba(255, 235, 134, .5)',
                        shadowBlur: 5,
                        shadowOffsetY: 5,
                    },
                    itemStyle: {
                        normal: {
                            color: colorList[4],
                            borderColor: colorList[4],
                        },
                    },
                },
            ],
        };
        echarts2.setOption(option2)
    },
};
</script>

<style scoped>
.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(21, 206, 123);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(235, 208, 31);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(240, 101, 76);
}

.grid-con-3 .grid-num {
    color: rgb(238, 88, 62);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 2px solid #ccc;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}
</style>
